package it.controllocredito.wizard.ui;

import it.controllocredito.R;
import it.controllocredito.wizard.model.DayInfoPage;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Spinner;

public class DayInfoFragment extends Fragment {
    private static final String ARG_KEY = "key";

    private PageFragmentCallbacks mCallbacks;
    private String mKey;
    private DayInfoPage mPage;
    private Spinner mDayView;

    LayoutInflater inflater = null;

    public static DayInfoFragment create(String key) {
	Bundle args = new Bundle();
	args.putString(ARG_KEY, key);

	DayInfoFragment fragment = new DayInfoFragment();
	fragment.setArguments(args);
	return fragment;
    }

    public DayInfoFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	Bundle args = getArguments();
	mKey = args.getString(ARG_KEY);
	mPage = (DayInfoPage) mCallbacks.onGetPage(mKey);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	this.inflater = inflater;
	View rootView = inflater.inflate(R.layout.wizard_fragment_page_day_info, container, false);
	((TextView) rootView.findViewById(android.R.id.title)).setText(mPage.getTitle());

	mDayView = (Spinner) rootView.findViewById(R.id.days_spinner);

	ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(rootView.getContext(), R.array.days_array, android.R.layout.simple_spinner_item);
	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	mDayView.setAdapter(adapter);

	return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
	super.onAttach(activity);

	if (!(activity instanceof PageFragmentCallbacks)) {
	    throw new ClassCastException("Activity must implement PageFragmentCallbacks");
	}

	mCallbacks = (PageFragmentCallbacks) activity;
    }

    @Override
    public void onDetach() {
	super.onDetach();
	mCallbacks = null;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
	super.onViewCreated(view, savedInstanceState);

	mDayView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
	    @Override
	    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		mPage.getData().putString(DayInfoPage.DAY_DATA_KEY, (parent != null) ? parent.getSelectedItem().toString() : null);
		mPage.notifyDataChanged();

	    }

	    @Override
	    public void onNothingSelected(AdapterView<?> parent) {
		Log.v("routes", "nothing selected");
	    }
	});

    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
	super.setMenuVisibility(menuVisible);

	// In a future update to the support library, this should override
	// setUserVisibleHint
	// instead of setMenuVisibility.
	if (mDayView != null) {
	    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
	    if (!menuVisible) {
		imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
	    }
	}
    }

}